<template>
  <div class="head-menu">
    <el-menu :default-active="activeIndex" class="el-menu-demo headercss">
      <el-menu-item @click="collapsMenu" class="fold">
        <i
          v-if="this.$store.state.isCollapse"
          :title="$t('shrinkage.unfold')"
          class="fold-unfold el-icon-s-fold"
        ></i>
        <i
          v-else
          :title="$t('shrinkage.fold')"
          class="fold-unfold el-icon-s-unfold"
        ></i>
      </el-menu-item>
      <el-menu-item class="refresh" @click="play">
        <i
          :class="{ rotate360: showAnimate }"
          @animationend="reset"
          class="el-icon-refresh"
        ></i>
      </el-menu-item>
      <el-menu-item
        v-for="(item, index) in headList"
        :key="item.menuId"
        class="navs"
        :class="theme === 'dark' ? 'navDark' : ''"
        :index="(index + 1).toString()"
        @click="getTree(item.children)"
        >{{ item.menuName }}<i :class="'iconfont ' + item.icon"></i
      ></el-menu-item>

      <el-menu-item
        style="height: 33px; line-height: 33px"
        :class="theme === 'dark' ? 'headrcssr' : 'headrcss'"
      >
        <div class="r-content">
          <!--用户信息-->
          <el-dropdown trigger="click" size="mini">
            <span
              :class="
                theme === 'dark'
                  ? 'el-dropdown-link ssxuserlink'
                  : 'el-dropdown-link'
              "
            >
              <span>{{ JSON.parse(this.$store.state.userInfo).uname }}</span>
              <i class="el-icon-user-solid"></i>
              <!--绑定图片-->
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="personal" icon="el-icon-user">{{
                $t("headernav.admin")
              }}</el-dropdown-item>
              <el-dropdown-item
                icon="el-icon-unlock"
                @click.native="pwVisible = true"
                >{{ $t("headernav.password") }}</el-dropdown-item
              >
              <el-dropdown-item @click.native="help" icon="el-icon-question">{{
                $t("headernav.help")
              }}</el-dropdown-item>
              <el-dropdown-item
                @click.native="open"
                icon="el-icon-switch-button"
                >{{ $t("headernav.logout") }}</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-menu-item>
    </el-menu>

    <!-- 修改密码弹出框 -->
    <el-dialog
      :title="$t('headernav.password')"
      :visible.sync="pwVisible"
      @keyup.native.enter="submit"
    >
      <el-form ref="pwform" :model="pwform" :rules="rules" status-icon>
        <el-form-item :label="$t('headernav.old')" prop="Oldpwd">
          <el-input type="password" v-model="pwform.Oldpwd"></el-input>
        </el-form-item>
        <el-form-item :label="$t('headernav.new')" prop="Newpwd">
          <el-input type="password" v-model="pwform.Newpwd"></el-input>
        </el-form-item>
        <el-form-item :label="$t('headernav.affirm')" prop="confirmPwd">
          <el-input type="password" v-model="pwform.confirmPwd"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" type="danger" @click="close">{{
          $t("public.cancelbtn")
        }}</el-button>
        <el-button size="small" type="primary" @click="submit">{{
          $t("public.savebtn")
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  inject: ["reload"],
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.pwform.Newpwd !== value) {
        callback(new Error(this.$t("headernav.isconsistent")));
      } else {
        callback();
      }
    };
    return {
      pwVisible: false,
      showAnimate: false,
      pwform: {
        Oldpwd: undefined,
        Newpwd: undefined,
        confirmPwd: undefined,
      },
      // 表单校验
      rules: {
        Oldpwd: [
          {
            required: true,
            message: this.$t("headernav.validateold"),
            trigger: "blur",
          },
        ],
        Newpwd: [
          {
            required: true,
            message: this.$t("headernav.validatenew"),
            trigger: "blur",
          },
          {
            min: 6,
            max: 20,
            message: this.$t("headernav.validatelong"),
            trigger: "blur",
          },
        ],
        confirmPwd: [
          {
            required: true,
            message: this.$t("headernav.validateaffirm"),
            trigger: "blur",
          },
          { required: true, validator: equalToPassword, trigger: "blur" },
        ],
      },
      activeIndex: "1",
      headList: [],
      theme: this.$store.state.theme, //主题切换  dark / light
    };
  },
  mounted() {
    this.getUsidMenu();
  },
  beforeUpdate() {},
  methods: {
    //获取
    async getUsidMenu() {
      try {
        let params = {
          usid: JSON.parse(this.$store.state.userInfo).uid,
          language: this.$store.state.language,
        };
        let res = await this.$api.welllist.getUsidMenu(params);
        this.headList = res.data;
        // this.headList = res.data.map((e,i) => {
        //   e.menuName = this.$t("public.handNav")[i]
        //   e.children.map((ee,ii) =>{
        //     ee.menuName = this.$t("public.leftNav")[ii]
        //     return ee
        //   })
        //   return e
        // })
        this.getTree(res.data[0].children);
      } catch (e) {
        console.log(e);
      }
    },
    collapsMenu() {
      this.$store.commit("collapseMenu");
    },
    //点击导航栏事件
    getTree(data) {
      this.$store.commit("navData", data);
    },
    open() {
      this.$confirm(
        this.$t("headernav.confirmcontent"),
        this.$t("public.confirmtitle"),
        {
          confirmButtonText: this.$t("public.savebtn"),
          cancelButtonText: this.$t("public.cancelbtn"),
          type: "warning",
        }
      )
        .then((res) => {
          this.$router.push({ path: "/login" });
          this.userexit();
        })
        .catch((err) => {
          console.log(err);
        });
    },
    async userexit() {
      try {
        let params = {
          usid: JSON.parse(this.$store.state.userInfo).uid,
        };
        await this.$api.login.userexit(params);
      } catch (e) {
        console.log(e);
      }
    },
    personal() {
      this.$router.push({ path: "/admin" });
    },
    help() {
      this.$router.push({ path: "/helpcenter" });
    },
    //保存密码
    submit() {
      this.$refs["pwform"].validate(async (valid) => {
        if (valid) {
          let params = {
            usid: JSON.parse(this.$store.state.userInfo).uid,
            Oldpwd: this.pwform.Oldpwd,
            Newpwd: this.pwform.Newpwd,
          };
          let res = await this.$api.management.editUpwd(params);
          if (res.code == 200) {
            this.$refs["pwform"].resetFields(); //清空表单
            this.pwVisible = false;
            this.$message.success(this.$t("headernav.editertips"));
          } else {
            this.$message({
              showClose: true,
              message: res.msg,
              type: "error",
            });
          }
        }
      });
    },
    //取消
    close() {
      this.$refs["pwform"].resetFields(); //清空表单
      this.pwVisible = false;
    },
    //刷新页面
    play() {
      this.showAnimate = true;
      this.reload();
    },
    reset() {
      this.showAnimate = false;
    },
  },
};
</script>
<style lang="less" scoped>
.headrcss {
  width: 95px;
  text-align: center;
  border-radius: 4px;
  padding-right: 6px;
}
.headrcssr {
  // width: 95px;
  text-align: center;
  border-radius: 4px;
  padding-right: 0px;
}
.el-menu-demo {
  display: flex;
  align-items: center;
  position: relative;
  .navDark{
    min-width: 155px;
    height: 36px;
    line-height: 36px;
    text-align: center;
    background: url(../../assets/navun.png) no-repeat;
    font-size: 16px;
    background-size: 100% 100%;
    padding-left: 30px!important;
    background-color: rgba(19, 32, 60, 0) !important;
    > i{padding-bottom: 3px;}
    &:hover {
      z-index: 99;
      background: url(../../assets/navchecked.png) no-repeat;
      background-size: 100% 100%;
    }
    &.is-active {
      background: url(../../assets/navchecked.png) no-repeat;
      background-size: 100% 100%;
    }
  }
  .el-menu-item {

    &:nth-child(3) {
      transform: translateX(0);
    }
    &:nth-child(4) {
      transform: translateX(-14px);
    }
    &:nth-child(5) {
      transform: translateX(-28px);
    }
    &:nth-child(6) {
      transform: translateX(-42px);
    }
    &:nth-child(7) {
      transform: translateX(-56px);
    }
    &:nth-child(8) {
      transform: translateX(-70px);
    }
    &:nth-child(9) {
      transform: translateX(-84px);
    }
    &:last-child {
      position: absolute;
      right: 0;
      // transform: translateX(0);
    }
  }

  .ssxuserlink {
    width: 145px;
    height: 35px;
    display: block;
    text-align: center;
    line-height: 35px;
    background: url(../../assets/admin.png) no-repeat;
    &:hover {
      background: url(../../assets/admin2.png) no-repeat;
    }
    > span {
      color: #ffffff;
      font-size: 16px;
      font-weight: bolder;
    }
  }
  .iconfont {
    padding-left: 10px;
  }
  .fold {
    &:hover {
      .fold-unfold {
        font-size: 30px;
      }
    }
    .el-icon-s-fold {
      transform: rotate(180deg);
      transition: all 0.6s;
    }
    .el-icon-s-unfold {
      transform: rotate(-180deg);
      transition: all 0.6s;
    }
  }
  .refresh {
    display: flex;
    align-items: center;
    .el-icon-refresh {
      display: block;
      font-size: 23px;
    }
    .rotate360 {
      animation: rotate360 0.6s ease-out 0s;
    }
    @keyframes rotate360 {
      100% {
        transform: rotate(360deg);
      }
    }
  }
}
</style>